<template>
	<view>
		<view class='shoppingCart'>
			<view class='labelNav'>
				<view class='item'><u-icon name="checkmark-circle"></u-icon></text>{{i18n.promise}}</view>
				<view class='item'><u-icon name="checkmark-circle"></u-icon>{{i18n.Allgoods}}</view>
				<view class='item'><u-icon name="checkmark-circle"></u-icon>{{i18n.After}}</view>
			</view>
			<view class='nav'>
				<view>{{i18n.Sun}} <text class='num font-color'>{{cartTotalCount}}</text></view>
				<view v-if="cartList.valid.length > 0 " class='administrate acea-row row-center-wrapper'
				 @click='manage'>{{ footerswitch ? i18n.Admin : i18n.Cancel}}</view>
			</view>
			<view v-if="cartList.valid.length > 0 || cartList.invalid.length > 0">
				<view class='list'>
					<block v-for="(item,index) in cartList.valid" :key="index">
						<view class='item acea-row row-between-wrapper'>
							<view class="store-title">
								<view class="checkbox" @click="storeAllCheck(item,index)">
									<text v-if="!item.allCheck" class="iconfont iconcaozuojiemiantubiao---_yuan"></text>
									<text v-else class="iconfont icon30xuanzhongyuanxingfill"></text>
								</view>
								<navigator class="info acea-row">
									<text class="iconfont icondianpu"></text>
									<view class="name">{{item.mer_name}} </view>
									<text class="iconfont iconxianxingxiangyou"></text>
								</navigator>
								<view class="coupon-btn" v-if="item.hasCoupon>0" @click="giveCoupon(item)">{{i18n.Discounts}}</view>
							</view>
							<navigator v-for="goods in item.list" :key="goods.cart_id" :url='"/pages/goods_details/index?id="+goods.product.product_id'
							 hover-class='none' class='picTxt acea-row'>
								<view class="checkbox" @click.stop="goodsCheck(goods,index)">
									<text v-if="!goods.check" class="iconfont iconcaozuojiemiantubiao---_yuan"></text>
									<text v-else class="iconfont icon30xuanzhongyuanxingfill"></text>
								</view>
								<view class='pictrue'>
									<image :src='goods.productAttr.image || goods.product.image'></image>
								</view>
								<view class='text'>
									<view class='line1'>{{goods.product.store_name}}</view>
									<view class='infor line1' v-if="goods.productAttr.sku">{{i18n.Rroperty}} : {{goods.productAttr.sku}}</view>
									<view class='money'>￥{{goods.productAttr.price}}</view>
								</view>
								<view class='carnum acea-row row-center-wrapper'>
									<view class="reduce" :class="goods.numSub ? 'on' : ''" @click.stop='subCart(goods)'>-</view>
									<view class='num'>{{goods.cart_num}}</view>
									<view class="plus" :class="goods.numAdd ? 'on' : ''" @click.stop='addCart(goods)'>+</view>
								</view>
							</navigator>
						</view>
					</block>
				</view>
				<view class='invalidGoods' v-if="cartList.invalid.length > 0">
					<view class='goodsNav acea-row row-between-wrapper'>
						<view @click='goodsOpen'><text class='iconfont' :class='goodsHidden==true?"iconxianxingxiangxia":"iconxianxingxiangshang"'>失效商品</text></view>
						<view class='del' @click='unsetCart'><text class='iconfont iconshanchu'></text>{{i18n.Clear}}</view>
					</view>
					<view class='goodsList' :hidden='goodsHidden'>
						<block v-for="(item,index) in cartList.invalid" :key='index'>
							<view class='item acea-row row-between-wrapper'>
								<view class='invalid'>{{i18n.Failure}}</view>
								<view class='pictrue'>
									<image :src='item.productAttr.image || item.product.image'></image>
				
								</view>
								<view class='text acea-row row-column-between'>
									<view class='line1 name'>{{item.product.store_name}}</view>
									<view class='acea-row row-between-wrapper'>
										<view class='end'>{{i18n.FailureGooods}}</view>
									</view>
								</view>
							</view>
						</block>
					</view>
				</view>
			</view>
			<view style="height: 60px;">
			</view>
			<view class='footer acea-row row-between-wrapper' v-if="cartList.valid.length > 0">
				<view>
					<view class="allcheckbox" @click.stop="checkboxAllChange">
						<text v-if="!isAllSelect" class="iconfont iconcaozuojiemiantubiao---_yuan"></text>
						<text v-else class="iconfont icon30xuanzhongyuanxingfill"></text>
						{{i18n.CheckAll}} ({{cartCount}})
					</view>
				</view>
				<view class='money acea-row row-middle' v-if="footerswitch==true">
					<text class='font-color'>￥{{selectCountPrice}}</text>
					<form @submit="subOrder" report-submit='true'>
						<button class='placeOrder bg-color' formType="submit">{{i18n.Place}}</button>
					</form>
				</view>
				<view class='button acea-row row-middle' v-else>
					<form @submit="subCollect" report-submit='true'>
						<button class='bnt cart-color' formType="submit">{{i18n.Attention}}</button>
					</form>
					<form @submit="subDel" report-submit='true'>
						<button class='bnt' formType="submit">{{i18n.Delete}}</button>
					</form>
				</view>
			</view>	
		</view>
		<!-- 优惠券弹窗 -->
		<block v-if="coupon.coupon">
			<couponListWindow 
				:coupon='coupon' 
				@ChangCouponsClone="ChangCouponsClone" 
				@ChangCouponsUseState="ChangCouponsUseState"
			></couponListWindow>
		</block> 
	</view>
</template>

<script>
	const globalData = getApp().globalData;
	import {getCartList,getCartCounts,changeCartNum,cartDel} from '@/api/order.js';
	import store from '@/store';
	import {mapGetters} from "vuex";
	import {collectAll} from '@/api/store.js';
	import {
		getCoupons,
		getShopCoupons
	} from '@/api/api.js';
	import couponListWindow from '@/components/couponListWindow';
	export default {
		components: {
			couponListWindow
		},
		data() {
			return {
				goodsHidden: true,
				footerswitch:true,
				cartCount: 0,
				selectCountPrice: 0.00,
				allCheck:false,
				isAllSelect: false, //全选
				cartTotalCount:0,
				selectValue: [], //选中的数据
				cartList: {
					valid: [],
					invalid: []
				},
				//属性是否打开
				coupon: {
					'coupon': false,
					list: [],
				},
			}
		},
		computed: {i18n() {
			return globalData.$t('order_addcart');
		},
		...mapGetters(['isLogin'])},
		methods: {
			// 失效商品展开
			goodsOpen: function() {
				let that = this;
				that.goodsHidden = !that.goodsHidden;
			},
			// 清空
			unsetCart: function() {
				let that = this,
					ids = [];
				for (let i = 0, len = that.cartList.invalid.length; i < len; i++) {
					ids.push(that.cartList.invalid[i].cart_id);
				}
				cartDel({
					cart_id:ids
				}).then(res => {
					that.$util.Tips({
						title:  that.i18n.Empty
					});
					that.$set(that.cartList, 'invalid', []);
				}).catch(res => {
			
				});
			},
			// 店铺优惠券
			giveCoupon(item){
				let that = this;
				let goodsArr = []
				let couponList = [];
				let activeList = [];
				let ids = []
				item.list.map(el=>{
					ids.push(el.product_id)
				})
				getCoupons({
					ids:ids.join(',')
				}).then(res => {
					goodsArr = res.data
					getShopCoupons(item.mer_id).then(({data})=>{
						uni.hideTabBar();
						couponList = goodsArr.concat(data)
						this.$set(this.coupon, 'list', couponList);
						this.$set(this.coupon, 'coupon', true);
					}).catch(error=>{
						uni.showTabBar();
					})
				});
			
			},
			ChangCouponsClone: function() {
				uni.showTabBar();
				this.$set(this.coupon, 'coupon', false)
			},
			ChangCouponsUseState(index) {
				uni.showTabBar();
				let that = this;
				that.coupon.list[index].issue = true;
			},
			// 立即下单
			subOrder: function(event) {
				let selectValue = []
				let isskip = "true"
				this.cartList.valid.forEach(el=>{
					el.list.forEach(goods=>{
						if(goods.check){
							selectValue.push(goods.cart_id)
						}
					}) 
				})
				if (selectValue.length > 0) {
					
					uni.navigateTo({
						url: '/pages/users/order_confirm/index?cartId=' + selectValue.join(',')
					});
				} else {
					return that.$util.Tips({
						title:  that.i18n.Shan
					});
				}
			},
			// 收藏
			subCollect: function(event) {
				let that = this
				let type_id = []
				this.cartList.valid.forEach(el=>{
					el.list.forEach(goods=>{
						if(goods.check){
							type_id.push(goods.product.product_id)
						}
					})
				})
				if(type_id.length == 0){
					return that.$util.Tips({
						title:  that.i18n.Shan
					});
				}else{
					collectAll({
						type_id:type_id,
						type:1
					}).then(res=>{
						return that.$util.Tips({
							title: res.message
						});
					}).catch(err => {
						return that.$util.Tips({
							title: err
						});
					});
				}
			},
			// 删除
			subDel: function(event) {
				let that = this
				let type_id = []
				this.cartList.valid.forEach(el=>{
					el.list.forEach(goods=>{
						if(goods.check){
							type_id.push(goods.cart_id)
						}
					})
				})
				if(type_id.length == 0){
					return that.$util.Tips({
						title: that.i18n.Shan
					});
				}else{
					cartDel({
						cart_id:type_id,
					}).then(res=>{
						this.getCartList();
						this.getCartNum();
						return that.$util.Tips({
							title: res.message,
							icon: 'success'
						});
					}).catch(err => {
						return that.$util.tips({
							title: err
						});
					});
				}
			},
			// 商铺全选
			storeAllCheck(item, index) {
				// 店铺取消
				if (item.allCheck) {
					item.allCheck = false
					item.list.forEach((el, index) => {
						el.check = false
					})
				} else {
					item.allCheck = true
					item.list.forEach((el, index) => {
						el.check = true
					})
				}
				this.cartAllCheck('goodsCheck')
			},
			// 商品选中
			goodsCheck(goods) {
				goods.check = !goods.check
				this.cartAllCheck('goodsCheck')
			},
			// 全选判断
			cartAllCheck(type) {
				let allArr = [];
				let totalMoney = 0
				let totalNum = 0
				this.cartList.valid.forEach((el, index) => {
					if (type == 'goodsCheck') {
						let tempArr = el.list.filter(goods => {
							return goods.check == true
						})
						if (el.list.length == tempArr.length) {
							el.allCheck = true
							allArr.push(el)
						} else {
							el.allCheck = false
						}
					} else {
						el.list.forEach((goods) => {
							goods.check = this.isAllSelect
						})
						el.allCheck = this.isAllSelect
						if (el.allCheck) allArr.push(el)
					}
					// 总金额 //总数
					el.list.forEach(e => {
						if (e.check) {
							totalMoney = this.$util.$h.Add(totalMoney, this.$util.$h.Mul(e.productAttr.price, e.cart_num))
							totalNum += e.cart_num
						}
					})
				})
				this.cartCount = totalNum
				this.selectCountPrice = totalMoney
				// 全选
				this.isAllSelect = allArr.length == this.cartList.valid.length ? true : false
			
			},
			// 购物车全选
			checkboxAllChange() {
				this.isAllSelect = !this.isAllSelect
				this.cartAllCheck('cartCheck')
			},
			// 购物车列表
			getCartList:function(){
				getCartList().then(res=>{
					res.data.list.forEach((item, index) => {
						item.allCheck = true
						item.list.forEach((goods, j) => {
							goods.check = true
							if (goods.cart_num == 1) {
								goods.numSub = true;
							} else {
								goods.numSub = false;
							}
							if (goods.cart_num == goods.productAttr.stock) {
								goods.numAdd = true;
							} else {
								goods.numAdd = false;
							}
					
						})
					})
					this.cartList.valid = res.data.list
					this.cartList.invalid = res.data.fail
					this.checkboxAllChange()
				}).catch(res=>{
					
				})
			},
			getCartNum:function(){
				let that = this;
				getCartCounts().then(res=>{
					that.cartTotalCount = res.data[0].count || 0;
				}).catch(res=>{
					
				})
			},
			addCart: function(goods, index) {
				let that = this;
				goods.cart_num = Number(goods.cart_num) + 1
				this.cartTotalCount = Number(this.cartTotalCount) + 1;
				if (goods.hasOwnProperty('productAttr') && goods.cart_num > goods.productAttr.stock) {
					goods.cart_num = goods.productAttr.stock;
					goods.numAdd = true;
					goods.numSub = false;
					return
				} else {
					goods.numAdd = false;
					goods.numSub = false;
				}
				changeCartNum(goods.cart_id, {
					cart_num: goods.cart_num
				}, 'test').then(res => {}).catch(error => {
					goods.cart_num = Number(goods.cart_num) - 1
				})
				this.cartAllCheck('goodsCheck')
			},
			subCart(goods) {
				let status = false;
				
				if (goods.cart_num < 1) status = true;
			
				if (goods.cart_num <= 1) {
					goods.cart_num = 1;
					goods.numSub = true;
					status = true;
					
				} else {
					goods.cart_num = Number(goods.cart_num) - 1
					this.cartTotalCount = Number(this.cartTotalCount) - 1;
					goods.numSub = false;
					goods.numAdd = false;
					if(goods.cart_num <= 1){
						goods.numSub = true;
					}
				}
				if (false == status) {
					changeCartNum(goods.cart_id, {
						cart_num: goods.cart_num
					}).then(res => {}).catch(error => {
						goods.cart_num = Number(goods.cart_num) - 1
					})
					this.cartAllCheck('goodsCheck')
				}
				
			},
			// 管理
			manage: function() {
				let that = this;
				that.footerswitch = !that.footerswitch;
			},
		},
		onLoad: function(options) {
			uni.setNavigationBarTitle({
				title: this.i18n.Title
			});
			let that = this;
			if (that.isLogin == false) {
				that.isAuto = true;
				that.$set(that, 'isShowAuth', true);
			}
		},
		onShow() {
			if (this.isLogin == true) {
				this.isShowAuth = false;
				this.getCartList();
				this.getCartNum();
			}
			this.cartList = {
				valid: [],
				invalid: []
			},
			this.footerswitch = true;
			this.isAllSelect = false; //全选
			this.selectValue = []; //选中的数据
			this.selectCountPrice = 0.00;
			this.cartCount = 0;
			this.isShowAuth = false;
		}
	}
</script>

<style lang="scss">
	.checkbox {
		width: 60rpx;
	
		.iconfont {
			font-size: 40rpx;
			color: #CCCCCC;
		}
	
		.icon30xuanzhongyuanxingfill {
			color: #E93323;
		}
	}
.shoppingCart{
	.labelNav{
		display: flex;
		flex-wrap: nowrap;
		align-items: center;
		justify-content: space-around;
		height: 76rpx;
		padding: 0 30rpx;
		font-size: 22rpx;
		color: #8c8c8c;
		position: fixed;
		width: 100%;
		box-sizing: border-box;
		background-color: #f5f5f5;
		z-index: 6;
		top: 0;
	}	
	.nav{
		display: flex;
		flex-wrap: nowrap;
		align-items: center;
		justify-content: space-between;
		width: 100%;
		height: 80rpx;
		background-color: #fff;
		padding: 0 30rpx;
		box-sizing: border-box;
		font-size: 28rpx;
		color: #282828;
		position: fixed;
		z-index: 5;
		top: 76rpx;
		.administrate {
			font-size: 26rpx;
			color: #282828;
			width: 110rpx;
			height: 46rpx;
			border-radius: 6rpx;
			border: 1px solid #868686;
		}
	}
	.list{
		margin-top: 171rpx;
		.item{
			background-color: #fff;
			margin-bottom: 15rpx;
			
			.store-title{
				display: flex;
				align-items: center;
				width: 100%;
				padding: 0 30rpx;
				height: 85rpx;
				border-bottom: 1px solid #f0f0f0;
				.info {
					flex: 1;
					display: flex;
					align-items: center;
				
					.iconfont {
						font-size: 36rpx;
					}
				
					.name {
						margin: 0 0 0 10rpx;
						font-size: 28rpx;
						color: #282828;
						font-weight: bold;
					}
				
					.iconxianxingxiangyou {
						margin-top: 6rpx;
						font-size: 22rpx;
						color: #999;
					}
				}
				.coupon-btn {
					color: #E93323;
					font-size: 22rpx;
					width: 100rpx;
					height: 36rpx;
					line-height: 36rpx;
					background: #FFEDEB;
					border-radius: 18rpx;
					text-align: center;
				}
			}
			.picTxt{
				width: 100%;
				padding: 25rpx 30rpx;
				position: relative;
				align-items: center;
				border-bottom: 1px solid #f0f0f0;
				.pictrue{
					width: 160rpx;
					height: 160rpx;
					image{
						width: 100%;
						height: 100%;
					}
				}
				.text{
					width: 444rpx;
					margin-left: 20rpx;
					font-size: 28rpx;
					color: #282828;
					.infor{
						font-size: 24rpx;
						color: #868686;
						margin-top: 16rpx;
					}
					.money{
						font-size: 32rpx;
						color: #282828;
						margin-top: 28rpx;
					}
				}
				.carnum{
					height: 47rpx;
					position: absolute;
					bottom: 30rpx;
					right: 30rpx;
					view{
						border: 1rpx solid #a4a4a4;
						width: 66rpx;
						text-align: center;
						height: 100%;
						line-height: 40rpx;
						font-size: 28rpx;
						color: #a4a4a4;
					}
					.reduce {
						border-right: 0;
						border-radius: 3rpx 0 0 3rpx;
					}
					.reduce.on {
						border-color: #e3e3e3;
						color: #dedede;
					}
					.plus {
						border-left: 0;
						border-radius: 0 3rpx 3rpx 0;
					}
					.num {
						color: #282828;
					}
				}
			}
		}
	}
	.footer {
		z-index: 9;
		width: 100%;
		height: 96rpx;
		background-color: #fafafa;
		position: fixed;
		padding: 0 30rpx;
		box-sizing: border-box;
		border-top: 1rpx solid #eee;
		bottom: var(--window-bottom);
		.checkAll {
			font-size: 28rpx;
			color: #282828;
			margin-left: 16rpx;
		}
		.money {
			font-size: 30rpx;
		}
		.placeOrder {
			color: #fff;
			font-size: 30rpx;
			width: 226rpx;
			height: 70rpx;
			border-radius: 50rpx;
			text-align: center;
			line-height: 70rpx;
			margin-left: 22rpx;
		}
		.button .bnt {
			font-size: 28rpx;
			color: #999;
			border-radius: 50rpx;
			border: 1px solid #999;
			width: 160rpx;
			height: 60rpx;
			text-align: center;
			line-height: 60rpx;
		}
		.button form~form {
			margin-left: 17rpx;
		}
	}
	.allcheckbox {
		display: flex;
		align-items: center;
		width: 260rpx;
	
		.iconfont {
			margin-right: 20rpx;
			font-size: 40rpx;
			color: #CCCCCC;
		}
	
		.icon30xuanzhongyuanxingfill {
			color: #E93323;
		}
	}
}
.shoppingCart .invalidGoods {
		background-color: #fff;
	}

	.shoppingCart .invalidGoods .goodsNav {
		width: 100%;
		height: 66rpx;
		padding: 0 30rpx;
		box-sizing: border-box;
		font-size: 28rpx;
		color: #282828;
	}

	.shoppingCart .invalidGoods .goodsNav .iconfont {
		color: #424242;
		font-size: 28rpx;
		margin-right: 17rpx;
	}

	.shoppingCart .invalidGoods .goodsNav .del {
		font-size: 26rpx;
		color: #999;
	}

	.shoppingCart .invalidGoods .goodsNav .del .icon-shanchu1 {
		color: #999;
		font-size: 33rpx;
		vertical-align: -2rpx;
		margin-right: 8rpx;
	}

	.shoppingCart .invalidGoods .goodsList .item {
		padding: 20rpx 30rpx;
		border-top: 1rpx solid #f5f5f5;
	}

	.shoppingCart .invalidGoods .goodsList .item .invalid {
		font-size: 22rpx;
		color: #fff;
		width: 70rpx;
		height: 36rpx;
		background-color: #aaa;
		border-radius: 3rpx;
		text-align: center;
		line-height: 36rpx;
	}

	.shoppingCart .invalidGoods .goodsList .item .pictrue {
		width: 140rpx;
		height: 140rpx;
	}

	.shoppingCart .invalidGoods .goodsList .item .pictrue image {
		width: 100%;
		height: 100%;
		border-radius: 6rpx;
	}

	.shoppingCart .invalidGoods .goodsList .item .text {
		width: 433rpx;
		font-size: 28rpx;
		color: #999;
		height: 140rpx;
	}

	.shoppingCart .invalidGoods .goodsList .item .text .name {
		width: 100%;
	}

	.shoppingCart .invalidGoods .goodsList .item .text .infor {
		font-size: 24rpx;
	}

	.shoppingCart .invalidGoods .goodsList .item .text .end {
		font-size: 26rpx;
		color: #bbb;
	}
</style>
